package com.tanhua.dubbo.api;


import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tanhua.dubbo.mappers.UserInfoMapper;
import com.tanhua.dubbo.mappers.UserMapper;
import com.tanhua.model.domain.User;
import com.tanhua.model.domain.UserInfo;
import com.tanhua.model.dto.BaseUserInfoDto;
import com.tanhua.model.dto.UserInfoDto;
import com.tanhua.model.vo.PageResult;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 用户基础信息的dubbo实现类
 */
@DubboService
public class UserInfoApiImpl implements UserInfoApi {

    @Autowired
    private UserInfoMapper userInfoMapper;

    @Autowired
    private UserMapper userMapper;

    @DubboReference
    private FriendApi friendApi;

    //更新
    @Override
    public void update(UserInfo userInfo) {
        userInfoMapper.updateById(userInfo);
    }

    @Override
    public UserInfo findById(Long id) {
        UserInfo userInfo = userInfoMapper.selectById(id);
        return userInfo;

    }

    @Override
    public void updateById(UserInfo userInfo) {
        userInfoMapper.updateById(userInfo);
    }

    @Override
    public Map<Long, UserInfo> findByIds(List<Long> ids, UserInfo info) {
        //构建构造器
        LambdaUpdateWrapper<UserInfo> lqw=new LambdaUpdateWrapper<>();
        //根据ids查询
        lqw.in(ids!=null,UserInfo::getId,ids);
        if(info!=null){
            lqw.lt(info.getAge()!=null,UserInfo::getAge,info.getAge());
            lqw.eq(info.getGender()!=null,UserInfo::getGender,info.getGender());
            lqw.like(info.getNickname()!=null,UserInfo::getNickname,info.getNickname());
        }
        //根据条件查询用户数据列表
        List<UserInfo> list = userInfoMapper.selectList(lqw);
        Map<Long, UserInfo> map = CollUtil.fieldValueMap(list, "id");
        return map;
    }

    @Override
    public PageResult select(Long id, String nickname, String city, Integer page, Integer pagesize) {
        Page pg = new Page(page, pagesize);
        LambdaQueryWrapper<UserInfo> queryWrapper1 = new LambdaQueryWrapper<>();
        queryWrapper1.like(id != null, UserInfo::getId, id);
        queryWrapper1.like(StringUtils.isNotEmpty(nickname), UserInfo::getNickname, nickname);
        queryWrapper1.like(StringUtils.isNotEmpty(city), UserInfo::getCity, city);
        Page<UserInfo> userInfoPage = userInfoMapper.selectPage(pg, queryWrapper1);
        List<UserInfo> userInfos = userInfoPage.getRecords();
        List<UserInfoDto> dtos = userInfos.stream().map(item -> {
            User user = userMapper.selectById(item.getId());
            UserInfoDto dto = new UserInfoDto();
            BeanUtils.copyProperties(item, dto);
            dto.setMobile(user.getMobile());
            dto.setOccupation(item.getProfession());
            dto.setLogo(item.getAvatar());
            dto.setSex(item.getGender());
            return dto;
        }).collect(Collectors.toList());
        PageResult pageResult = new PageResult(page, pagesize, (long) userInfoPage.getTotal(), dtos);
        return pageResult;
    }

    @Override
    public BaseUserInfoDto selectOne(Long id) {
        UserInfo userInfo = userInfoMapper.selectById(id);
        User user = userMapper.selectById(userInfo.getId());
        BaseUserInfoDto dto = new BaseUserInfoDto();
        BeanUtils.copyProperties(userInfo, dto);
        dto.setMobile(user.getMobile());
        dto.setOccupation(userInfo.getProfession());
        dto.setLogo(userInfo.getAvatar());
        dto.setSex(userInfo.getGender());
        dto.setCountMatching(friendApi.findCountMatching(id));
        dto.setCountBeLiked(friendApi.findBeLiked(id));
        dto.setCountLiked(friendApi.findLiked(id));
        dto.setCreated((int) userInfo.getCreated().getTime());
        return dto;
    }

    //保存
    @Override
    public void save(UserInfo userInfo) {
        userInfoMapper.insert(userInfo);
    }
}